<?php
namespace common\modules\taxonomy;

use common\modules\taxonomy\models\Taxonomy;
use common\modules\taxonomy\models\TaxonomyCategory;
use common\core\modularity\ModuleServiceInterface;
use yii\helpers\ArrayHelper;

class TaxonomyService implements ModuleServiceInterface
{

    public function getServiceId()
    {
        return 'taxonomyService';
    }

    public function getTaxonomyCategories()
    {
        $categories = TaxonomyCategory::findAll([], 'name asc');
        return $categories;
    }

    public function getTaxonomiesAsTree($category)
    {
        return Taxonomy::getArrayTree($category);
    }

    public function getTaxonomyById($id, $default = true)
    {
        $taxonomyModel = Taxonomy::getTaxonomyById($id);
        if ($taxonomyModel === null && $default === true) {
            $taxonomyModel = new Taxonomy();
            $taxonomyModel->id = - 1;
            $taxonomyModel->name = '所有';
        }
        return $taxonomyModel;
    }

    public function getModel($model)
    {
        $items = [
            'Taxonomy' => Taxonomy::className(),
            'TaxonomyCategory' => TaxonomyCategory::className()
        ];
        return ArrayHelper::getItems($items, $model, true);
    }
}
